package org.NooLab.itexx.app.gui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import org.jdesktop.swingx.JXEditorPane;



/*
 *  http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JEditorPane.html
 *  
 *  see demo class: ExampleHtmlDisplay 
 */

/**
 * 
 * 
 * 
 */
public class TransparentTextPane extends JXEditorPane{// EditorPane {
	
	Image importedImage;
	float transparency;
	
	public TransparentTextPane(Image img, float transparency, String text) { // 
		super();
		
		this.transparency = transparency;
		importedImage = img;
		setText(text);
		setOpaque(false); //false   
		setBackground(new Color(0f,0.0f,0f,0.5f));
		
		
	}

	
	
	
	public void setPaneStyle(){
		/*
		StyledDocument doc= this.getStyledDocument();
		MutableAttributeSet attr= new SimpleAttributeSet();
		StyleConstants.setLineSpacing(attr, -0.2f); //NOTE: negative value.
		 */
	}
	
	
	
	
	// paint is executed before paintComponent ?
	//@Override
	public void _paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g.create();
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));// transparency));//)); 
		/*
		 * 
		 * AlphaComposite
		 *  - SRC_IN : filled, black text hulls, area dark gray
		 *  - SRC_OVER: text not visible, image visible
		 *  - DST_OVER: text not visible, image not visible
		 *  - DST_IN : image area black
		 */
		super.paint(g2);  
	}
	
    @Override
    protected void paintComponent(Graphics g) {
        
        try {
            //
            // Load an image for the background image of out JTextPane.
            // importedImage


        	int w,h;
            w = (int) getSize().getWidth();
            h = (int) getSize().getHeight();
            
            g.drawImage( getImage(g), 0, 0, w,h, this);   
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        
        super.paintComponent(g);    
    }
    
     
	private Image getImage(Graphics g) throws IOException{
    	BufferedImage img;
    	// image = ImageIO.read(getClass().getResourceAsStream("org/NooLab/itexx/app/gui/resources/button-glassy background out.png"));
		String imgResource ="org/NooLab/itexx/app/gui/resources/button-glassy background out.png" ;
		org.NooLab.utilities.resources.ResourceLoader rL = new org.NooLab.utilities.resources.ResourceLoader();
		
		boolean hb = rL.loadImageResource( this.getClass(), imgResource) ;
		
		if (hb){
			img=null;
			img = rL.getImageResource() ;
		}else{
			Graphics2D g2d = (Graphics2D)g;
			img = g2d.getDeviceConfiguration().createCompatibleImage( (int) getSize().getWidth(),
                													  (int) getSize().getHeight(),Transparency.TRANSLUCENT);
		}
		return img;
    }
}